﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.Kinect.Nui;

namespace KiMoCSys.core
{
    /// <summary>
    /// Represents a kinect device connected in the current computer system.
    /// </summary>
    class KinectDevice
    {
        private Runtime device;
        private int index;

        public int DeviceIndex { get { return index; } }

        public Runtime Device
        {
            get { return device; }
            set { device = value; }
        }

        public KinectDevice(Runtime kinect,RuntimeOptions options,int index)
        {
            device = kinect;
            this.index = index;
            device.Initialize(options);
            device.SkeletonEngine.TransformSmooth = true;
            device.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(onSkeletonFrameReady);
        }

        private void onSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Console.WriteLine("KinectDevice::onSkeletonFrameReady - " + index);            
            SkeletonFrame skl = e.SkeletonFrame;
            if (skl == null)
                return;
            
            
            FrameSkeleton frmSkeleton = new FrameSkeleton();
            FrameSkeletonNode node = null;
            foreach (SkeletonData data in skl.Skeletons)
            {
                node = new FrameSkeletonNode(data.Joints[JointID.AnkleLeft].Position.X, data.Joints[JointID.AnkleLeft].Position.Y, data.Joints[JointID.AnkleLeft].Position.Z);
                frmSkeleton.AnkleLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.AnkleRight].Position.X, data.Joints[JointID.AnkleRight].Position.Y, data.Joints[JointID.AnkleRight].Position.Z);
                frmSkeleton.AnkleRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.ElbowLeft].Position.X, data.Joints[JointID.ElbowLeft].Position.Y, data.Joints[JointID.ElbowLeft].Position.Z);
                frmSkeleton.ElbowLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.ElbowRight].Position.X, data.Joints[JointID.ElbowRight].Position.Y, data.Joints[JointID.ElbowRight].Position.Z);
                frmSkeleton.ElbowRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.FootLeft].Position.X, data.Joints[JointID.FootLeft].Position.Y, data.Joints[JointID.FootLeft].Position.Z);
                frmSkeleton.FootLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.FootRight].Position.X, data.Joints[JointID.FootRight].Position.Y, data.Joints[JointID.FootRight].Position.Z);
                frmSkeleton.FootRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.HandLeft].Position.X, data.Joints[JointID.HandLeft].Position.Y, data.Joints[JointID.HandLeft].Position.Z);
                frmSkeleton.HandLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.HandRight].Position.X, data.Joints[JointID.HandRight].Position.Y, data.Joints[JointID.HandRight].Position.Z);
                frmSkeleton.HandRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.Head].Position.X, data.Joints[JointID.Head].Position.Y, data.Joints[JointID.Head].Position.Z);
                frmSkeleton.Head = node;

                node = new FrameSkeletonNode(data.Joints[JointID.HipCenter].Position.X, data.Joints[JointID.HipCenter].Position.Y, data.Joints[JointID.HipCenter].Position.Z);
                frmSkeleton.HipCenter = node;

                node = new FrameSkeletonNode(data.Joints[JointID.HipLeft].Position.X, data.Joints[JointID.HipLeft].Position.Y, data.Joints[JointID.HipLeft].Position.Z);
                frmSkeleton.HipLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.HipRight].Position.X, data.Joints[JointID.HipRight].Position.Y, data.Joints[JointID.HipRight].Position.Z);
                frmSkeleton.HipRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.KneeLeft].Position.X, data.Joints[JointID.KneeLeft].Position.Y, data.Joints[JointID.KneeLeft].Position.Z);
                frmSkeleton.KneeLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.KneeRight].Position.X, data.Joints[JointID.KneeRight].Position.Y, data.Joints[JointID.KneeRight].Position.Z);
                frmSkeleton.KneeRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.ShoulderCenter].Position.X, data.Joints[JointID.ShoulderCenter].Position.Y, data.Joints[JointID.ShoulderCenter].Position.Z);
                frmSkeleton.ShoulderCenter = node;

                node = new FrameSkeletonNode(data.Joints[JointID.ShoulderLeft].Position.X, data.Joints[JointID.ShoulderLeft].Position.Y, data.Joints[JointID.ShoulderLeft].Position.Z);
                frmSkeleton.ShoulderLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.ShoulderRight].Position.X, data.Joints[JointID.ShoulderRight].Position.Y, data.Joints[JointID.ShoulderRight].Position.Z);
                frmSkeleton.ShoulderRight = node;

                node = new FrameSkeletonNode(data.Joints[JointID.Spine].Position.X, data.Joints[JointID.Spine].Position.Y, data.Joints[JointID.Spine].Position.Z);
                frmSkeleton.Spine = node;

                node = new FrameSkeletonNode(data.Joints[JointID.WristLeft].Position.X, data.Joints[JointID.WristLeft].Position.Y, data.Joints[JointID.WristLeft].Position.Z);
                frmSkeleton.WristLeft = node;

                node = new FrameSkeletonNode(data.Joints[JointID.WristRight].Position.X, data.Joints[JointID.WristRight].Position.Y, data.Joints[JointID.WristRight].Position.Z);
                frmSkeleton.WristRight = node;

                KinectDeviceManager.notifyNewFrame(this, frmSkeleton);
            }
        }





    }
}
